#include <json/json.h>
#include "NxModelService.h"
#include "../utils/NxHttp.h"

const char* NJGIS::SERVICE::NjModelService::getServiceOID()
{
	return this->_oid.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServiceName()
{
	return this->_name.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServiceType()
{
	return this->_type.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServiceDetailURL()
{
	return this->_url.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServicePid()
{
	return this->_pid.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServiceMid()
{
	return this->_mid.c_str();
}

bool NJGIS::SERVICE::NjModelService::getServiceRegister()
{
	return this->_registered;
}

const char* NJGIS::SERVICE::NjModelService::getServiceDescription()
{
	return this->_description.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServiceVersion()
{
	return this->_version.c_str();
}

const NJGIS::SERVICE::NjPlatform NJGIS::SERVICE::NjModelService::getServicePlatform()
{
	return this->_platform;
}

const char* NJGIS::SERVICE::NjModelService::getDeploymentTime()
{
	return this->_deploymentTime.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getImage()
{
	return this->_img.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServiceDeployorName()
{
	return this->_deployorName.c_str();
}

const char* NJGIS::SERVICE::NjModelService::getServiceDeployorEmail()
{
	return this->_deployorEmail.c_str();
}

NJGIS::SERVICE::NjModelServiceStatus NJGIS::SERVICE::NjModelService::getServiceStatus()
{
	return this->_status;
}

NJGIS::SERVICE::NjModelServiceLimitation NJGIS::SERVICE::NjModelService::getServiceLimitation()
{
	return this->_limit;
}

const NJGIS::SERVICE::NjModelServicePermission NJGIS::SERVICE::NjModelService::getServicePermission()
{
	return this->_permission;
}

int NJGIS::SERVICE::NjModelService::invoke( INjDataConfiguration* config, std::string& recordid )
{
	std::string url;
	this->getBaseUrl(url);

	url += "modelser/";
	url += this->getServiceOID();
	url += "?ac=run&inputdata=[";

	for(int i = 0 ; i < config->getCount(); i++)
	{
		std::string state;
		std::string event;
		std::string dataid;
		bool destoryed;
		if(config->getData(i, state, event, dataid, destoryed) > 0)
		{
			url += "{\"StateId\":\"" + state + "\",\"Event\":\"" + event + "\",\"DataId\":\"" + dataid + "\",\"Destoryed\":\"false\"}";
		}
		else
		{
			continue;
		}
	}
	url += "]&auth=";

	Json::Value jRespones;
	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jRespones);
	if(jRespones["result"].asString() == "suc")
	{
		recordid = jRespones["data"].asString();
		return 1;
	}
	else if(jRespones["result"].asString() == "error")
	{
		return -1;
	}
	return 0;
}

int NJGIS::SERVICE::NjModelService::refresh()
{
	//! TODO DX 
	std::string url;
	Json::Value jResponse;
	this->getBaseUrl(url);

	url += "modelser/json/";
	url += std::string(this->getServiceOID());

	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(),jResponse);

	if(jResponse["result"].asString() == "suc"){
		Json::Value jMs = jResponse["data"];
		this->_status = (NJGIS::SERVICE::NjModelServiceStatus)jMs["ms_status"].asInt();
		this->_limit = (NJGIS::SERVICE::NjModelServiceLimitation)jMs["ms_limited"].asInt();
		this->_permission = (NJGIS::SERVICE::NjModelServicePermission)jMs["ms_permission"].asInt();
	}else{
	   return -1;
	}

	return 1;
	
}
